package com.hrms.controller.admin;

import com.hrms.annotation.AdminIgnore;
import com.hrms.entity.Admin;
import com.hrms.http.ResponseResult;
import com.hrms.service.AdminService;
import com.hrms.utils.EntityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Transactional
@RestController
@RequestMapping("/admin/index")
public class AdminIndexController {
    // 管理员首页

    @Autowired
    private AdminService adminService;

    @AdminIgnore
    @RequestMapping("/login")
    public ResponseResult<Object> login(@RequestBody EntityUtil entity) {
        // 管理员登录

        Admin admin = new Admin();
        admin.setAdmin_id(entity.getAdmin_id());
        admin.setAdmin_password(entity.getAdmin_password());

        // 账号是否存在
        if(!adminService.accountExist(admin)) {
            // 回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseResult<>("ERROR_ACCOUNT", null);
        }

        // 账号密码是否匹配
        if(!adminService.passwordisRight(admin)) {
            // 回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseResult<>("ERROR_PASSWORD", null);
        }

        return new ResponseResult<>("SUCCESS", null);
    }
}
